﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace text
{
    /// HS_Phone phone = new HS_Phone();
    ///phone.letterCombinations("23");
    ///Console.WriteLine();
    /// <summary>
    /// 17.电话号码的字母组合
    /// </summary>
    public class HS_Phone
    {
        public List<String> ReStr= new List<string>();
        public Dictionary<string, string> phone = new Dictionary<string, string> {
                {"2", "abc"},
                {"3", "def"},
                {"4", "ghi"},
                {"5", "jkl"},
                {"6", "mno"},
                {"7", "pqrs"},
                {"8", "tuv"},
                {"9", "wxyz"}
            };
        /// <summary>
        /// 获得所有可能
        /// </summary>
        /// <param name="purpose">需要转换的目标</param>
        /// <param name="current">最终得到的值</param>
        public void backtrack(string purpose, string current)
        {
            if (purpose.Length==0)
            {
                ReStr.Add(current);
            }
            else
            {
                String digit = purpose.Substring(0, 1);
                String letters = phone[digit];
                //var str=phone[purpose.Substring(0, 1)]; 
                for (int i = 0; i < letters.Length; i++)
                {
                    String letter = letters.Substring(i,1);
                    backtrack(purpose.Substring(1), current+letter);
                }
            }
        }
        public List<String> letterCombinations(String digits)
        {
            if (digits.Length!= 0)
                backtrack(digits,"");
            return ReStr;
        }
    }
}
